import smtplib
from email.mime.text import MIMEText
import requests
#from requests.packages.urllib3.exceptions import InsecureRequestWarning
from bs4  import BeautifulSoup

# 北京
def getweather():
    yburl = 'https://free-api.heweather.com/s6/weather/forecast'
    value = {
        'location': '洛阳',
        'key': 'cc5ee6f8072142f586e84be670babec5',
        'lang': 'zh'
    }
    ybreq = requests.get(yburl, params=value)
    ybjs = ybreq.json()

    # 返回api参数:
    #print(ybjs)

    for i in range(2):
        yb = ybjs['HeWeather6'][0]['daily_forecast']
        d1 = yb[i]['date'] + ' ' + yb[i]['cond_txt_d'] + ' ' + yb[i]['tmp_min'] + '—' + yb[i]['tmp_max'] + '℃' + ' ' + \
             yb[i]['wind_dir'] + ' ' + yb[i]['wind_sc'] + '级'
        # output(d1)
    # qq推送
    print(d1)
    return d1

def getword():
    url = "https://chp.shadiao.app/api.php"
    content = requests.get(url, verify=False).content.decode("utf-8", errors="ignore")
    soup = BeautifulSoup(content, 'html.parser')
    content = soup.string
    return content

if __name__ == '__main__':
    msg_from = '1480111487@qq.com'  # 发送方邮箱
    passwd = 'ombyrwvsugghjhia'  # 填入发送方邮箱的授权码(填入自己的授权码，相当于邮箱密码)
    msg_to = ['2991608067@qq.com']  # 收件人邮箱
    subject = "来清茶淡饭四季相伴的消息！❤"  # 主题
    # 生成一个MIMEText对象（还有一些其它参数）
    # _text_:邮件内容
    content = getweather() + "\n" + getword()
    print(content)
    msg = MIMEText(content)
    # 放入邮件主题
    msg['Subject'] = subject
    # 也可以这样传参
    # msg['Subject'] = Header(subject, 'utf-8')
    # 放入发件人
    msg['From'] = msg_from
    # 放入收件人
    msg['To'] = '送给最幸运的你'
    # msg['To'] = '发给你的邮件啊'
    try:
        # 通过ssl方式发送，服务器地址，端口
        s = smtplib.SMTP_SSL("smtp.qq.com", 465)
        # 登录到邮箱
        s.login(msg_from, passwd)
        # 发送邮件：发送方，收件方，要发送的消息
        s.sendmail(msg_from, msg_to, msg.as_string())
        print('成功')
    except s.SMTPException as e:
        print(e)
    finally:
        s.quit()

